from django.core.paginator import Paginator
from django.shortcuts import render
from django.http import  HttpResponse

# Create your views here.
from blog.models import Article

#博客内容;
def article_content(request):
    article = Article.objects.all()[0]
    title = article.title
    brief_content = article.brief_content
    content = article.content
    article_id = article.article_id
    publish_date = article.publish_date

    return_str = 'title: %s, brief_content: %s, ' \
                 'content: %s, article_id: %s, publish_date: %s' % (title,
                                                                    brief_content,
                                                                    content,
                                                                    article_id,
                                                                    publish_date)
    return HttpResponse(return_str)

#首页内容
def get_index_page(request):
    #获取页码参数
    page = request.GET.get('page')
    if page:
        page = int(page)
    else:
        page = 1
    print('page param:',page)

    #获取所有的信息
    all_article = Article.objects.all()
    #通过切片和发布日期倒序获取前10条记录;
    top10_article_list = Article.objects.order_by('-publish_date')[:10]

    #分页器实例对象(参数1：待分页的对象列表；参数2：每页数量);
    paginator = Paginator(all_article,6)
    #获取总页数量;
    page_num = paginator.num_pages
    print('page num:',page_num)
    #通过页码获取页面列表清单;
    page_article_list = paginator.page(page)
    #判断是否有下一页;
    if page_article_list.has_next():
        next_page = page+1
    else:
        next_page = page

    #判断是否有上一页;
    if page_article_list.has_previous():
        previous_page = page -1
    else:
        previous_page = page

    return render(request,'blog/index.html',
                      {
                        'article_list': page_article_list,
                          'page_num': range(1,page_num+1),
                          'curr_page': page,
                          'next_page': next_page,
                          'previous_page': previous_page,
                          'top10_article_list': top10_article_list
                      }
                  )

#根据id获取博客详细信息;
def get_detail_page(request,article_id):
    all_article = Article.objects.all()
    curr_article = None
    previous_index = 0
    next_index = 0
    previous_article = None
    next_article = None

    #对于一个可迭代的（iterable）/可遍历的对象（如列表、字符串），
    # enumerate将其组成一个索引序列，利用它可以同时获得索引和值
    for index,article in enumerate(all_article):
        if index == 0:
            previous_index = 0
            next_index = index +1
        elif index == len(all_article) - 1:
            previous_index = index-1
            next_index = index
        else:
            previous_index = index -1
            next_index = index+1

        #通过id判断当前记录;
        if article.article_id == article_id:
            curr_article = article
            previous_article = all_article[previous_index]
            next_article = all_article[next_index]
            break

    #通过换行符拆分成记录列表;
    #影响的结果，需传递json参数;
    section_list = curr_article.content.split('\n')
    return render(request,'blog/detail.html',
                  {
                      'curr_article':curr_article,
                      'section_list':section_list,
                      'previous_article': previous_article,
                      'next_article': next_article
                  }
                  )
